package com.tangly.codeing.service.impl;

import com.tangly.codeing.dao.SysUserDao;
import com.tangly.codeing.dao.SysUserRoleDao;
import com.tangly.codeing.model.SysUser;
import com.tangly.codeing.model.SysUserRole;
import com.tangly.codeing.model.dto.SysUserDto;
import com.tangly.codeing.service.SysUserRoleService;
import com.tangly.codeing.service.SysUserService;
import com.tangly.codeing.utils.IdWoker;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import com.tangly.codeing.utils.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户表
 *
 * @author tangbing
 * @date 2019-04-28
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private IdWoker idWoker;

    /**
     * 新增
     */
    @Override
    public ReturnT<String> insert(SysUser sysUser) {

        // valid
        if (sysUser == null) {
            return new ReturnT<>(ReturnT.FAIL_CODE, "必要参数缺失");
        }
        sysUser.setUuid(idWoker.nextStringId());
        sysUserDao.insert(sysUser);
        return ReturnT.SUCCESS;
    }

    /**
     * 删除
     */
    @Override
    public ReturnT<String> delete(int id) {
        int ret = sysUserDao.delete(id);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> update(SysUserDto sysUserDto) {
        // 更新用户基本信息
        sysUserDao.update(sysUserDto);
        // 更新用户角色信息
        List<Long> roleIdList = sysUserDto.getRoleList();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        roleIdList.forEach(roleId -> sysUserRoleList.add(new SysUserRole(sysUserDto.getId(), roleId)));
        sysUserRoleService.delete(sysUserDto.getId());
        sysUserRoleService.saveOrUpdate(sysUserRoleList);
        return ReturnT.SUCCESS;
    }

    /**
     * Load查询
     */
    @Override
    public SysUser load(int id) {
        return sysUserDao.load(id);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo pageList(PageInfo pageInfo) {
        List<SysUser> pageList = sysUserDao.pageList(pageInfo);
        int totalCount = sysUserDao.pageListCount(pageInfo);

        pageInfo.setResultObject(pageList);
        pageInfo.setTotalRecord(totalCount);
        return pageInfo;
    }

    @Override
    public List<SysUser> findAll(PageInfo pageInfo) {
        return sysUserDao.findAll(pageInfo);
    }

    @Override
    public SysUser findByUuid(String uuid) {
        return sysUserDao.findByUuid(uuid);
    }

    /**
     * 批量删除
     */
    @Override
    public ReturnT<String> batchDelete(List<String> ids) {
        int ret = sysUserDao.batchDelete(ids);
        return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
    }

}
